package model

import (
	"fmt"
	"project2/g"
	"time"
)

type SysAccount struct {

	// 主键ID
	Id uint `json:"id" gorm:"primaryKey;column:id"`

	// 登录用户名
	UserName string `json:"userName" gorm:"column:user_name"`

	// 登录密码
	PassWord string `json:"passWord" gorm:"column:pass_word"`

	// 累计登录次数
	LoginNum uint `json:"loginNum" gorm:"column:login_num"`

	// 账号状态[数据字典=AccountState]
	State uint `json:"state" gorm:"column:state"`

	// 在线状态[数据字典=OnlieState]
	OnlieState uint `json:"onlieState" gorm:"column:onlie_state"`

	// 是否为超级管理员[查询]
	IsAdmin uint `json:"isAdmin" gorm:"column:is_admin"`

	// ip锁定状态[查询]0未锁定1己锁定
	IpState uint `json:"ipState" gorm:"column:ip_state"`

	// IP锁定可支持多个IP用逗号隔开
	Ip string `json:"ip" gorm:"column:ip"`

	// 最近登录时间
	LastLoginTime time.Time `json:"lastLoginTime" gorm:"column:last_login_time"`

	// 最近一次登录IP
	LastIp string `json:"lastIp" gorm:"column:last_ip"`

	// 租户类型
	TenantType uint `json:"tenantType" gorm:"column:tenant_type"`

	// 租户ID
	TenantId uint `json:"tenantId" gorm:"column:tenant_id"`

	// 有效状态[数据字典=del_state]
	DelState uint `json:"delState" gorm:"column:del_state"`

	// 添加人
	AddUser string `json:"addUser" gorm:"column:add_user"`

	// 添加人ID
	AddUserId uint `json:"addUserId" gorm:"column:add_user_id"`

	// 添加时间
	AddTime time.Time `json:"addTime" gorm:"column:add_time"`

	// 最近更新人
	EditUser string `json:"editUser" gorm:"column:edit_user"`

	// 最近更新人ID
	EditUserId uint `json:"editUserId" gorm:"column:edit_user_id"`

	// 最近更新时间
	LastEditTime time.Time `json:"lastEditTime" gorm:"column:last_edit_time"`

	// 服务器编号ID
	ServerNumber uint `json:"serverNumber" gorm:"column:server_number"`

	//
	DelTime   time.Time  `json:"delTime" gorm:"column:del_time"`
	CreatedAt time.Time  `json:"createdAt" gorm:"column:created_at"`
	UpdatedAt time.Time  `json:"updatedAt" gorm:"column:updated_at"`
	DeletedAt *time.Time `json:"deletedAt" gorm:"column:deleted_at"`
}

func (SysAccount) TableName() string {
	return "sys_account"
}
func (self *SysAccount) UpdateEnity(id int, updatelog *string) {

	var oldData SysAccount
	g.Db.Model(&SysAccount{}).Where("id = ?", id).First(&oldData)
	if oldData.Id <= 0 {
		//todo error
		return
	}

	updateTx := g.Db.Model(&SysAccount{}).Where("id = ?", id)
	updateEnity := &SysAccount{}

	if self.Id != oldData.Id {
		updateEnity.Id = self.Id
		(*updatelog) += fmt.Sprintf("主键ID从 %s 修改为 %s", oldData.Id, self.Id)
	}

	if self.UserName != oldData.UserName {
		updateEnity.UserName = self.UserName
		(*updatelog) += fmt.Sprintf("登录用户名从 %s 修改为 %s", oldData.UserName, self.UserName)
	}

	if self.PassWord != oldData.PassWord {
		updateEnity.PassWord = self.PassWord
		(*updatelog) += fmt.Sprintf("登录密码从 %s 修改为 %s", oldData.PassWord, self.PassWord)
	}

	if self.LoginNum != oldData.LoginNum {
		updateEnity.LoginNum = self.LoginNum
		(*updatelog) += fmt.Sprintf("累计登录次数从 %s 修改为 %s", oldData.LoginNum, self.LoginNum)
	}

	if self.State != oldData.State {
		updateEnity.State = self.State
		(*updatelog) += fmt.Sprintf("账号状态[数据字典=AccountState]  1正常  5己停用从 %s 修改为 %s", oldData.State, self.State)
	}

	if self.OnlieState != oldData.OnlieState {
		updateEnity.OnlieState = self.OnlieState
		(*updatelog) += fmt.Sprintf("在线状态[数据字典=OnlieState]从 %s 修改为 %s", oldData.OnlieState, self.OnlieState)
	}

	if self.IsAdmin != oldData.IsAdmin {
		updateEnity.IsAdmin = self.IsAdmin
		(*updatelog) += fmt.Sprintf("是否为超级管理员[查询]从 %s 修改为 %s", oldData.IsAdmin, self.IsAdmin)
	}

	if self.IpState != oldData.IpState {
		updateEnity.IpState = self.IpState
		(*updatelog) += fmt.Sprintf("ip锁定状态[查询]0未锁定1己锁定从 %s 修改为 %s", oldData.IpState, self.IpState)
	}

	if self.Ip != oldData.Ip {
		updateEnity.Ip = self.Ip
		(*updatelog) += fmt.Sprintf("IP锁定可支持多个IP用逗号隔开从 %s 修改为 %s", oldData.Ip, self.Ip)
	}

	if self.LastLoginTime != oldData.LastLoginTime {
		updateEnity.LastLoginTime = self.LastLoginTime
		(*updatelog) += fmt.Sprintf("最近登录时间从 %s 修改为 %s", oldData.LastLoginTime, self.LastLoginTime)
	}

	if self.LastIp != oldData.LastIp {
		updateEnity.LastIp = self.LastIp
		(*updatelog) += fmt.Sprintf("最近一次登录IP从 %s 修改为 %s", oldData.LastIp, self.LastIp)
	}

	if self.TenantType != oldData.TenantType {
		updateEnity.TenantType = self.TenantType
		(*updatelog) += fmt.Sprintf("租户类型 1应用管理者2应用消息者从 %s 修改为 %s", oldData.TenantType, self.TenantType)
	}

	if self.TenantId != oldData.TenantId {
		updateEnity.TenantId = self.TenantId
		(*updatelog) += fmt.Sprintf("租户ID从 %s 修改为 %s", oldData.TenantId, self.TenantId)
	}

	if self.DelState != oldData.DelState {
		updateEnity.DelState = self.DelState
		(*updatelog) += fmt.Sprintf("有效状态[数据字典=del_state] 1有效  5删除从 %s 修改为 %s", oldData.DelState, self.DelState)
	}

	if self.AddUser != oldData.AddUser {
		updateEnity.AddUser = self.AddUser
		(*updatelog) += fmt.Sprintf("添加人从 %s 修改为 %s", oldData.AddUser, self.AddUser)
	}

	if self.AddUserId != oldData.AddUserId {
		updateEnity.AddUserId = self.AddUserId
		(*updatelog) += fmt.Sprintf("添加人ID从 %s 修改为 %s", oldData.AddUserId, self.AddUserId)
	}

	if self.AddTime != oldData.AddTime {
		updateEnity.AddTime = self.AddTime
		(*updatelog) += fmt.Sprintf("添加时间从 %s 修改为 %s", oldData.AddTime, self.AddTime)
	}

	if self.EditUser != oldData.EditUser {
		updateEnity.EditUser = self.EditUser
		(*updatelog) += fmt.Sprintf("最近更新人从 %s 修改为 %s", oldData.EditUser, self.EditUser)
	}

	if self.EditUserId != oldData.EditUserId {
		updateEnity.EditUserId = self.EditUserId
		(*updatelog) += fmt.Sprintf("最近更新人ID从 %s 修改为 %s", oldData.EditUserId, self.EditUserId)
	}

	if self.LastEditTime != oldData.LastEditTime {
		updateEnity.LastEditTime = self.LastEditTime
		(*updatelog) += fmt.Sprintf("最近更新时间从 %s 修改为 %s", oldData.LastEditTime, self.LastEditTime)
	}

	if self.ServerNumber != oldData.ServerNumber {
		updateEnity.ServerNumber = self.ServerNumber
		(*updatelog) += fmt.Sprintf("服务器编号ID从 %s 修改为 %s", oldData.ServerNumber, self.ServerNumber)
	}

	if self.DelTime != oldData.DelTime {
		updateEnity.DelTime = self.DelTime
		(*updatelog) += fmt.Sprintf("从 %s 修改为 %s", oldData.DelTime, self.DelTime)
	}

	updateTx.Updates(updateEnity)

}
func (self *SysAccount) BeforeCreate() (err error) {
	self.CreatedAt = time.Now()
	self.AddTime = time.Now()
	return

}
func (self *SysAccount) BeforeUpdate() (err error) {
	self.LastEditTime = time.Now()
	return

}
